#include "xbram.h"
#include "xgpio.h"
#include "xparameters.h"
#include "sleep.h"

#define BRAM_BASE XPAR_AXI_BRAM_CTRL_0_S_AXI_BASEADDR
#define GPIO_READ_CH 2
#define GPIO_WRITE_CH 1

// #define RCV_BYTES 2
#define RCV_BYTES 320

XGpio pin = {0};
int main()
{
    u32 foo = 0;
    u32 i = 0;
    u32 err_cnt = 0;
    u32 rcv_cnt = 0;
    u32 rcv_data[RCV_BYTES] = {0};

    XGpio_Initialize(&pin, XPAR_AXI_GPIO_0_DEVICE_ID);
    XGpio_DiscreteWrite(&pin, GPIO_WRITE_CH, 0);

    for (i = 0; i < RCV_BYTES; i++)
    {
        XBram_WriteReg(BRAM_BASE, (i * 4), (0xCCDD0000 + i));
    }

    for (i = 0; i < RCV_BYTES; i++)
    {
        rcv_data[i] = XBram_ReadReg(BRAM_BASE, (i * 4));
    }

    while (1)
    {
        foo = XGpio_DiscreteRead(&pin, GPIO_READ_CH);
        if (foo > 0) // DATA VALID
        {

            XGpio_DiscreteWrite(&pin, GPIO_WRITE_CH, 1);
            XGpio_DiscreteWrite(&pin, GPIO_WRITE_CH, 0);

            for (i = 0; i < RCV_BYTES; i++)
            {
                rcv_data[i] = XBram_ReadReg(BRAM_BASE, (i * 4));
            }

            rcv_cnt++;
            
            for (i = 0; i < RCV_BYTES; i++)
            {
                if (rcv_data[i] != 0x6d)
                {
                    ++err_cnt;
                }
                
            }
            
        }
    }
}
